package top.wilbert.collegelink.forum.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.wilbert.collegelink.common.enums.TargetTypeEnum;
import top.wilbert.collegelink.common.utils.Query;
import top.wilbert.collegelink.forum.entity.CommentEntity;
import top.wilbert.collegelink.forum.service.CommentService;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;

import top.wilbert.collegelink.forum.service.TalkService;
import top.wilbert.collegelink.forum.vo.CommentCreateVo;
import top.wilbert.collegelink.forum.vo.CommentResponseVo;


/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2023-02-28 20:44:25
 */
@RestController
@RequestMapping("forum/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private TalkService talkService;


    /**
     * 发表评论
     *
     * @param commentCreateVo
     * @return
     */
    @PostMapping("/comment")
    public R comment(@RequestBody CommentCreateVo commentCreateVo) {
        commentService.comment(commentCreateVo);

        return R.ok();
    }

    /**
     * 用户删除评论
     *
     * @param commentId
     * @param pid
     * @return
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestParam Long commentId, @RequestParam Long pid) {
        commentService.reduceCommentCount(commentId);
        if (pid != 0) {
            CommentEntity commentEntity1 = commentService.getById(pid);
            commentEntity1.setCommentCount(commentEntity1.getCommentCount() - 1);
            commentService.updateById(commentEntity1);
        }
        commentService.removeById(commentId);

        return R.ok();
    }

    /**
     * 根据目标id和目标类型获取一级评论列表
     *
     * @param params
     * @param targetId
     * @param userId
     * @return
     */
    @RequestMapping("/listLevel1ByTalkId")
    public R listLevel1ByTalkId(@RequestParam Map<String, Object> params, @RequestParam Long targetId, @RequestParam Integer targetType, @RequestParam Long userId) {
        PageUtils page = commentService.listLevel1ByTalkId(new Query<CommentResponseVo>().getPage(params), targetId, targetType, userId);
        if(targetType == TargetTypeEnum.TALK.getTargetType()){
            //获取一级评论，说明用户点击进入该话题，话题浏览数+1
            talkService.addViewCount(targetId);
        }

        return R.ok().setData(page);
    }

    /**
     * 根据pid获取二级评论列表
     *
     * @param params
     * @param pid
     * @param userId
     * @return
     */
    @RequestMapping("/listLevel2ByPid")
    public R listLevel2ByPid(@RequestParam Map<String, Object> params, @RequestParam Long pid, @RequestParam Long userId) {
        PageUtils page = commentService.listLevel2ByTalkId(new Query<CommentResponseVo>().getPage(params), pid, userId);
        return R.ok().setData(page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("forum:comment:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = commentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{commentId}")
//    @RequiresPermissions("forum:comment:info")
    public R info(@PathVariable("commentId") Long commentId) {
        CommentEntity comment = commentService.getById(commentId);

        return R.ok().put("comment", comment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("forum:comment:save")
    public R save(@RequestBody CommentEntity comment) {
        commentService.save(comment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("forum:comment:update")
    public R update(@RequestBody CommentEntity comment) {
        commentService.updateById(comment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("forum:comment:delete")
    public R delete(@RequestBody Long[] commentIds) {
        commentService.removeByIds(Arrays.asList(commentIds));

        return R.ok();
    }

}
